package com.hama.iotrust.common.mybatis.criteria;

import java.util.HashMap;
import java.util.Map;

import com.hama.iotrust.common.mybatis.criteria.Criteria;
import com.hama.iotrust.common.mybatis.criteria.CriteriaException;
import com.hama.iotrust.common.mybatis.criteria.Expression;

/**
 * 逻辑表达式，AND | OR
 * 
 * @author 金建强(ptma@163.com)
 * @version 1.0.0
 * @since 2012-3-15 下午05:54:28
 */
public final class LogicExpression implements Expression {

    public enum Operator {
        AND, OR
    };

    private Expression m_lhp;
    private Expression m_rhp;
    private Operator   m_op;

    LogicExpression(Expression lhp, Operator operator, Expression rhp){
        m_lhp = lhp;
        m_rhp = rhp;
        m_op = operator;
    }

    public String getSql() {
        StringBuilder sb = new StringBuilder();
        sb.append("(").append(m_lhp.getSql()).append(" ").append(m_op.toString()).append(" ").append(m_rhp.getSql()).append(")");
        return sb.toString();
    }

    public Map<String, Object> getParameter() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.putAll(m_lhp.getParameter());
        map.putAll(m_rhp.getParameter());
        return map;
    }

    public boolean verify(Criteria<?> criteria) {
        return m_lhp.verify(criteria) && m_lhp.verify(criteria);
    }

    public void bind(Criteria<?> crit) throws CriteriaException {
        m_lhp.bind(crit);
        m_rhp.bind(crit);
    }
}
